<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\CouponLog;
use app\common\model\Coupon as CouponModel;

class Coupon extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';


    /**
     * 优惠券列表
     */
    public function list(){
        $user=$this->auth->getUser();

        $status=request()->param('status');
        $page=request()->param('page')?:1;

        $where=[
            ['user_id','=',$user['id']]
        ];
        if($status=='usable'){
            $where[]=['expire_time','>=',time()];
            $where[]=['use_time','null',''];
        }
        elseif($status=='used'){
            $where[]=['use_time','>',0];
        }
        elseif($status=='expired'){
            $where[]=['expire_time','<',time()];
            $where[]=['use_time','null',''];
        }

        $list=CouponLog::with(['coupon'])
            ->where($where)
            ->order('expire_time asc')
            ->page($page,10)
            ->select();

        $this->success('成功',$list);
    }
}